﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;

namespace wdss.WebServices
{
    /// <summary>
    /// Summary description for EmailClientService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class EmailClientService : System.Web.Services.WebService
    {

        public EmailClientService()
        {
            //InitializeComponent();
        }
        public bool sendMail(string username)
        {
            bool safeMode = false;
            string newPass;
            try
            {
                newPass = CreateRandomPassword();
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true;
                MailMessage message = new MailMessage();
                message.To.Add(username);
                message.Body = "Your new password is:\n"+newPass+"\nPlease change it after logging in.\nThank You\nSystem Admin\n";
                message.From = new MailAddress("1rn07cs@gmail.com");
                message.Subject = "New password generated";
                client.Credentials = new NetworkCredential("1rn07cs@gmail.com", "ka05hf11");
                client.Send(message);
                safeMode = true;
            }
            catch (Exception ex)
            {
                return safeMode;
            }
            SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            SqlCommand command = new SqlCommand("sp_ChangePassword", connect);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.Add("@par_username", System.Data.SqlDbType.NVarChar).Value = username;
            command.Parameters.Add("@par_password", System.Data.SqlDbType.NVarChar).Value = newPass;
            try
            {
                connect.Open();
                command.ExecuteNonQuery();
                safeMode = true;
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.StackTrace);
                return safeMode;
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.StackTrace);
                return safeMode;
            }
            return safeMode;

        }
        private static string CreateRandomPassword()
        {
            string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
            char[] chars = new char[9];
            Random rd = new Random();

            for (int i = 0; i < 6; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }
            chars[6] = '@';
            chars[7] = '5';
            chars[8] = 'w';
            return new string(chars);
        }
    }
}
